/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);
export const flattenData = data => {
    const result = [];
    for(let i = 0; i < data.length; i++) {
        const {usedParts = [], ...props} = data[i];
        usedParts.forEach(item => {
            result.push({
                ...item,
                ...props
            });
        });
    }
    return result;
};
/**
 *  原始数据转化为支持单元格行合并的数据
 *  使用场景 清单映射清单时，主单字段单元格合并
 *  @data 原表格数据
 *  @groupByKey 合并数据的key (主单数据的Id)
 *  @rowSpanKeyList 需要合并的字段 (编号、名称、操作列等)
 */
import {groupBy} from 'lodash';
export const generateRowSpanData = (data, groupByKey, rowSpanKeyList) => {
    const details = flattenData(data);
    const groupByData = groupBy(details, groupByKey);
    const result = [];
    for(const key in groupByData) {
        const array = groupByData[key];
        array.forEach((item, index) => {
            if(index === 0) {
                item.rowSpan = array.length;
                result.push(item);
            } else {
                rowSpanKeyList.forEach(rowSpanKey => {
                    delete item[rowSpanKey];
                });
                result.push(item);
            }
        });
    }
    return result;
};

import {formatMessage} from './intl';
export const validateAdd = (data, id) => {
    const msgs = [];
    if(!data.brandId && !id)
        msgs.push(
            formatMessage({
                id: 'validateAdd.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!data.vin)
        msgs.push(
            formatMessage({
                id: 'validateAdd.vin',
                defaultMessage: 'vin'
            })
        );
    if(!data.engineModel)
        msgs.push(
            formatMessage({
                id: 'validateAdd.engineModel',
                defaultMessage: '发动机型号'
            })
        );
    if(!data.productionDate)
        msgs.push(
            formatMessage({
                id: 'validateAdd.productionDate',
                defaultMessage: '生产日期'
            })
        );
    if(!data.saleDate)
        msgs.push(
            formatMessage({
                id: 'validateAdd.saleDate',
                defaultMessage: '销售日期'
            })
        );
    if(!data.consultType)
        msgs.push(
            formatMessage({
                id: 'validateAdd.consultType',
                defaultMessage: '咨询类型'
            })
        );
    if(!data.faultDate)
        msgs.push(
            formatMessage({
                id: 'validateAdd.faultDate',
                defaultMessage: '故障日期'
            })
        );
    if(!data.faultItemId)
        msgs.push(
            formatMessage({
                id: 'validateAdd.faultItemId',
                defaultMessage: '故障代码'
            })
        );
    if(!data.mileage)
        msgs.push(
            formatMessage({
                id: 'validateAdd.mileage',
                defaultMessage: '故障里程'
            })
        );
    if(!data.supplierId)
        msgs.push(
            formatMessage({
                id: 'validateAdd.supplierId',
                defaultMessage: '供应商'
            })
        );
    if(!data.transmissionType)
        msgs.push(
            formatMessage({
                id: 'validateAdd.transmissionType',
                defaultMessage: '变速器类型'
            })
        );
    if(!data.faultDescription)
        msgs.push(
            formatMessage({
                id: 'validateAdd.faultDescription',
                defaultMessage: '故障描述'
            })
        );
    if(!data.measureEffects)
        msgs.push(
            formatMessage({
                id: 'validateAdd.measureEffects',
                defaultMessage: '已采取措施及效果'
            })
        );
    if(!data.suggestion)
        msgs.push(
            formatMessage({
                id: 'validateAdd.suggestion',
                defaultMessage: '相关建议'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'validateAdd.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'validateAdd.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};
export const validateFeedback = data => {
    const {dealMethod, analysisResults} = data;
    const msgs = [];
    if(!dealMethod)
        msgs.push(
            formatMessage({
                id: 'validateFeedback.dealMethod',
                defaultMessage: '处理方式'
            })
        );
    if(!analysisResults)
        msgs.push(
            formatMessage({
                id: 'validateFeedback.analysisResults',
                defaultMessage: '分析结果'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'validateFeedback.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'validateFeedback.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};
export const validateReportAgain = (details, attachments) => {
    const emptyDetailErrors = [];
    if(!details.replyComments)
        emptyDetailErrors.push(formatMessage({
            id: 'validateReportAgain.empty.replyComments',
            defaultMessage: '再上报原因'
        }));
    if(attachments.length < 1)
        emptyDetailErrors.push(formatMessage({
            id: 'validateReportAgain.empty.attachments',
            defaultMessage: '附件'
        }));
    if(emptyDetailErrors.length)
        return `${emptyDetailErrors.join(
            formatMessage({
                id: 'validateReportAgain.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'validateReportAgain.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};
export const validateReply = (details, attachments) => {
    const emptyDetailErrors = [];
    if(!details.replyComments)
        emptyDetailErrors.push(formatMessage({
            id: 'validateReply.empty.replyComments',
            defaultMessage: '回复意见'
        }));
    if(attachments.length === 0)
        emptyDetailErrors.push(formatMessage({
            id: 'validateReply.empty.attachments',
            defaultMessage: '附件'
        }));
    if(emptyDetailErrors.length)
        return `${emptyDetailErrors.join(
            formatMessage({
                id: 'validateReply.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'validateReply.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    return false;
};
